package com.nullpointer.playerinfo.common.objects;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;


import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.nullpointer.playerinfo.common.Base;

public class Player {

    @DatabaseField(canBeNull = false, dataType = DataType.STRING)
    String name;

    @DatabaseField(id = true, canBeNull = false, dataType = DataType.UUID)
    UUID UUID;

    @DatabaseField(canBeNull = false, dataType = DataType.LONG)
    long firstPlayTime;

    @DatabaseField(canBeNull = false, dataType = DataType.LONG)
    long lastPlayTime;

    @DatabaseField(canBeNull = false, dataType = DataType.INTEGER)
    int level;

    @DatabaseField(canBeNull = false, dataType = DataType.INTEGER)
    int exp;

    @DatabaseField(canBeNull = false, dataType = DataType.INTEGER)
    int cash;

    @DatabaseField(canBeNull = false, dataType = DataType.INTEGER)
    int onlineTime;

    @DatabaseField(canBeNull = false, dataType = DataType.INTEGER)
    int totalOnlineTime;

    int PlayerTimerTaskId;


    public Player() {}

    public Player(String name, UUID UUID) {
        this.name = name;
        this.UUID = UUID;
        this.exp = 0;
        this.level = 0;
        this.cash = 0;
        this.onlineTime = 0;
        this.totalOnlineTime = 0;
        this.firstPlayTime = System.currentTimeMillis();
        this.lastPlayTime = System.currentTimeMillis();
    }

    public UUID getPlayerUUID() {
        return this.UUID;
    }

    public void setFirstPlayTime(long t) {
        this.firstPlayTime = t;
    }

    public void setLastPlayTime(long t) {
        this.lastPlayTime = t;
    }

    public String getFirstPlayTimeFormatted() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(this.firstPlayTime));
    }

    public String getLastPlayTimeFormatted() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(this.lastPlayTime));
    }

    public long getFirstPlayTime() {
        return this.firstPlayTime;
    }

    public long getLastPlayTime() {
        return this.lastPlayTime;
    }

    public void setTotalOnlineTime(int t) {
        this.totalOnlineTime = t;
    }

    public int getTotalOnlineTime() {
        return this.totalOnlineTime;
    }

    public String getOnlineTimeFomatted() {
        int hours = this.onlineTime / 3600;
        int minutes = (this.onlineTime % 3600) * 60;
        int seconds = this.onlineTime % 60;

        return String.format("%d:%d:%d", hours, minutes, seconds);
    }

    public Long getTotalOnlineTimeFomatted() {
        Long hours = ((long) this.onlineTime) / 3600;
        return hours;
    }

    public void setOnlineTime(int t) {
        this.onlineTime = t;
    }

    public int getOnlineTime() {
        return this.onlineTime;
    }

    public void setPlayerTimerTaskId(int id) {
        this.PlayerTimerTaskId = id;
    }

    public int getPlayerTimerTaskId() {
        return this.PlayerTimerTaskId;
    }

    public void setExp(int nub) {
        this.exp = nub;
    }

    public int getExp() {
        return this.exp;
    }

    public void setLevel(int level) {
        if (this.level > Base.maxLevel) return;
        this.level = level;
    }

    public int getLevel() {
        return this.level;
    }

    public void setCash(int cash) {
        this.cash = cash;
    }

    public int getCash() {
        return this.cash;
    }

    public void addExp(int e) {
        if (this.level == Base.maxLevel) return;
        this.exp = this.exp + e;
        if (!Base.useOriginalLevel) {
            if (getUpgradeExp() == 0) {
                this.level = Base.maxLevel;
                return;
            }
            while (this.exp >= getUpgradeExp() && this.level < Base.maxLevel) {
                this.exp = this.exp - getUpgradeExp();
                this.level++;
            }
        }
    }

    public void removeExp(int e) {
        this.exp = this.exp - e;
        if (!Base.useOriginalLevel) {
            while (this.exp != 0 && this.exp >= getBackExp()) {
                this.exp = this.exp - getBackExp();
                this.level--;
            }
        }
    }

    public void addCash(int cash) {
        this.cash = this.cash + cash;
    }

    public void removeCash(int cash) {
        this.cash = this.cash - cash;
    }

    public int getUpgradeExp() {
        for (int index = 0; index < Base.scopeMap.size(); index++) { // 遍历整个scopeMap
            List<Integer> scope = Base.scopeMap.get(index + 1);
            for (int i = scope.get(0); i <= scope.get(1); i++) { // 遍历区间
                if (this.level == i && scope.get(1) != Base.maxLevel) {
                    if (this.level == scope.get(1)) {
                        return Base.expMap.get(index + 2);
                    }
                    return Base.expMap.get(index + 1);
                }
            }
        }
        return 0;
    }

    public int getBackExp() {
        for (int index = 0; index < Base.scopeMap.size(); index++) { // 遍历整个scopeMap
            List<Integer> scope = Base.scopeMap.get(index + 1);
            for (int i = scope.get(0); i <= scope.get(1); i++) { // 遍历区间
                if (this.level - 1 == i) {
                    if (this.level - 1 == scope.get(1)) {
                        return Base.expMap.get(index + 2);
                    }
                    return Base.expMap.get(index + 1);
                }
            }
        }
        return 0;
    }


    public String getName() {
        return this.name;
    }

    public void removeData() {
        this.cash = 0;
        this.exp = 0;
        this.level = 0;
    }
}
